package com.chenggong.log.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.chenggong.log.LogLevel
import com.chenggong.log.LogRecord
import com.chenggong.log.R

@SuppressLint("SetTextI18n")
class ViewLogAdapter: RecyclerView.Adapter<ViewLogAdapter.ViewHolder>() {
    private var isPure: Boolean = false
    private var currentTag: String? = null
    private var currentLevel: String? = null
    var itemClickListener:((view: View, data: LogRecord, position: Int)->Unit)? = null
    var tagClickListener:((view: View, data: LogRecord, position: Int)->Unit)? = null
    var levelClickListener:((view: View, data: LogRecord, position: Int)->Unit)? = null
    private var listAll = ArrayList<LogRecord>()
    private var list = ArrayList<LogRecord>()

    @SuppressLint("NotifyDataSetChanged")
    fun setList(list: List<LogRecord>) {
        this.listAll.addAll(list)
        this.list.addAll(list)
        notifyDataSetChanged()
    }

    fun updateData(logRecord: LogRecord){
        listAll.add(logRecord)
        if(currentTag == null || currentLevel == null){
            list.add(logRecord)
        }else if(currentTag ==logRecord.tag){
            list.add(logRecord)
        }else if(currentLevel ==logRecord.level.tag){
            list.add(logRecord)
        }
        notifyItemInserted(list.size-1)
    }

    @SuppressLint("NotifyDataSetChanged")
    fun clear(){
        listAll.clear()
        list.clear()
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun filter(tag: String){
        if(tag == currentTag){
            currentTag = null
            list.clear()
            this.list.addAll(listAll)
        }else{
            currentTag = tag
            list.clear()
            listAll.forEach {
                if(it.tag == tag){
                    list.add(it)
                }
            }
        }
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun filter2(level: String){
        if(level == currentLevel){
            currentLevel = null
            list.clear()
            this.list.addAll(listAll)
        }else{
            currentLevel = level
            list.clear()
            listAll.forEach {
                if(it.level.tag == level){
                    list.add(it)
                }
            }
        }
        notifyDataSetChanged()
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        var holder = ViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.layout_item_log, parent, false)
        )
        holder.itemView.setOnClickListener {
            itemClickListener?.invoke(
                holder.itemView,
                list[holder.layoutPosition],
                holder.layoutPosition
            )
        }
        holder.tvTag.setOnClickListener {
            tagClickListener?.invoke(
                holder.itemView,
                list[holder.layoutPosition],
                holder.layoutPosition
            )
        }
        holder.tvLevel.setOnClickListener {
            levelClickListener?.invoke(
                holder.itemView,
                list[holder.layoutPosition],
                holder.layoutPosition
            )
        }
        return holder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var data = list[position]
        if(data.level == LogLevel.E){
            holder.tvTime.setTextColor(Color.parseColor("#ff0000"))
            holder.tvTag.setTextColor(Color.parseColor("#ff0000"))
            holder.tvLevel.setTextColor(Color.parseColor("#ff0000"))
            holder.tvMsg.setTextColor(Color.parseColor("#ff0000"))
        }else if(data.level == LogLevel.V){
            holder.tvTime.setTextColor(Color.parseColor("#7946e0"))
            holder.tvTag.setTextColor(Color.parseColor("#7946e0"))
            holder.tvLevel.setTextColor(Color.parseColor("#7946e0"))
            holder.tvMsg.setTextColor(Color.parseColor("#7946e0"))
        }else if(data.level == LogLevel.I){
            holder.tvTime.setTextColor(Color.parseColor("#46e083"))
            holder.tvTag.setTextColor(Color.parseColor("#46e083"))
            holder.tvLevel.setTextColor(Color.parseColor("#46e083"))
            holder.tvMsg.setTextColor(Color.parseColor("#46e083"))
        }else if(data.level == LogLevel.W){
            holder.tvTime.setTextColor(Color.parseColor("#46a0e0"))
            holder.tvTag.setTextColor(Color.parseColor("#46a0e0"))
            holder.tvLevel.setTextColor(Color.parseColor("#46a0e0"))
            holder.tvMsg.setTextColor(Color.parseColor("#46a0e0"))
        }else{
            holder.tvTime.setTextColor(Color.parseColor("#888888"))
            holder.tvTag.setTextColor(Color.parseColor("#888888"))
            holder.tvLevel.setTextColor(Color.parseColor("#888888"))
            holder.tvMsg.setTextColor(Color.parseColor("#000000"))
        }
        holder.layoutBase.isVisible = !isPure
        holder.tvTime.text = data.time
        holder.tvTag.text = data.tag
        holder.tvLevel.text = data.level.tag
        holder.tvMsg.text = data.msg
    }

    override fun getItemCount(): Int {
        return list.size
    }

    fun getAllContent(): String {
        val stringBuilder = StringBuffer()
        list.forEach {
            stringBuilder.append(it.time)
            stringBuilder.append("  ")
            stringBuilder.append(it.level.tag)
            stringBuilder.append("  ")
            stringBuilder.append(it.tag)
            stringBuilder.append("\n")
            stringBuilder.append(it.msg)
            stringBuilder.append("\n")
        }

        return stringBuilder.toString()
    }

    fun changeDisplay() {
        isPure = !isPure
        notifyDataSetChanged()
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val layoutBase: View
        val tvTime: TextView
        val tvTag: TextView
        val tvMsg: TextView
        val tvLevel: TextView

        init {
            layoutBase = itemView.findViewById(R.id.layout_base)
            tvTime = itemView.findViewById(R.id.tv_time)
            tvTag = itemView.findViewById(R.id.tv_tag)
            tvMsg = itemView.findViewById(R.id.tv_msg)
            tvLevel = itemView.findViewById(R.id.tv_level)
        }
    }

}